package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
/**
 * 大攻略资源控制器
 */
@RestController
@RequestMapping("strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private ServletContext servletContext;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @GetMapping
    public List list(StrategyQueryObject qo){
        return strategyService.selectAll(qo);
    }

    @GetMapping("{regionId}")
    public void query(StrategyQueryObject qo, HttpServletResponse response) throws IOException, TemplateException {
        //获得某个地区下的推荐攻略
        qo.setPageSize(0);//不分页
        qo.setState(Strategy.STATE_HOT);//推荐的状态
        PageInfo hot = strategyService.query(qo);//查询推荐的攻略
        //获取某个地区下的所有攻略
        qo.setPageSize(4);//分页
        qo.setState(null);
        qo.setNotDisable(true);
        PageInfo all = strategyService.query(qo);

        //需要渲染的数据
        HashMap root = new HashMap<>();
        root.put("hot",hot);
        root.put("all",all);

        //创建freemarker配置对象
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
        cfg.setDefaultEncoding("utf-8");
        //指定模板文件所在的目录
        File templates = new File(servletContext.getRealPath("/templates"));
        cfg.setDirectoryForTemplateLoading(templates);

        //获取模板文件
        Template template = cfg.getTemplate("test.ftl");
        //合并模板和数据
        response.setContentType("text/html;charset=utf-8");
        template.process(root,response.getWriter());

    }
    @GetMapping(value = "{regionId}",headers = "Accept=application/json")
    public PageInfo queryForList(StrategyQueryObject qo) {
        qo.setNotDisable(true);
        return strategyService.queryNoReasonable(qo);
    }

    @GetMapping("strategy/{strategyId}")
    public Strategy get(@PathVariable Long strategyId) {
        strategyService.updateBrowseNumById(strategyId);//增加浏览量,注:因前端模板特殊
        return strategyService.get(strategyId);
    }


    /**
     * 根据大攻略id查询评论数据
     * 1.资源 /strategies/{id}/comments
     * 2.请求方式 get
     * 3.请求参数 路径占位符
     * 4.返回结果 PageInfo
     * @param qo
     * @return
     */
    @GetMapping("{strategyId}/comments")
    public PageInfo selectContentById(StrategyCommentQueryObject qo){
        qo.setOrderBy("sc.createTime desc");
        return strategyCommentService.query(qo);
    }

    @PostMapping("{strategy.id}/comments")
    public JsonResult saveOrUpdateComment(StrategyComment comment, String[] tags){
        JsonResult jsonResult = new JsonResult();
        strategyCommentService.saveOrUpdate(comment,tags);
        return jsonResult;
    }

    //查询攻略的收藏数
    @GetMapping("collections/{strategyId}")
    public int CountForCollection(@PathVariable Long strategyId){
        return strategyService.CountForCollection(strategyId);
    }

}
